package com.bjpowernode.mvc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bjpowernode.common.client.DfsClient;
import com.bjpowernode.common.enums.RespCode;
import com.bjpowernode.db.domain.HeaderImage;
import com.bjpowernode.db.mapper.HeaderImageMapper;
import com.bjpowernode.mvc.model.dto.SaveHeaderImageDTO;
import com.bjpowernode.mvc.model.dto.UploadFileDTO;
import com.bjpowernode.mvc.model.result.CommonResult;
import com.bjpowernode.mvc.service.inter.UploadFileService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;

@RequiredArgsConstructor
@Service
public class UploadFileServiceImpl implements UploadFileService {

    private final DfsClient dfsClient;
    private final HeaderImageMapper headerImageMapper;

    /**
     * 上传用户头像
     *
     * @param uploadFileDTO
     */
    @Override
    public void uploadFileDFS(UploadFileDTO uploadFileDTO) {
        dfsClient.uploadFile(uploadFileDTO);
    }

    /**
     * 保存用户头像
     *
     * @param uploadFileDTO
     * @return
     */
    @Override
    public SaveHeaderImageDTO insertOrUploadFile(UploadFileDTO uploadFileDTO) {

        SaveHeaderImageDTO saveHeaderImageDTO = new SaveHeaderImageDTO();

        //1、查询头像是否存在
        QueryWrapper<HeaderImage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uploadFileDTO.getUid());
        HeaderImage headerImage = headerImageMapper.selectOne(queryWrapper);
        if (headerImage == null) {
            //头像不存在，直接insert
            headerImage = new HeaderImage();
            //文件路径名称
            headerImage.setFileId(uploadFileDTO.getFieldId());
            //fdfs组名称
            headerImage.setGroupName(uploadFileDTO.getGroup());
            //扩展名
            headerImage.setExtName(uploadFileDTO.getExt());
            //头像高度
            headerImage.setImgHeight(uploadFileDTO.getHeight());
            //头像宽度
            headerImage.setImgWidth(uploadFileDTO.getWidth());
            //用户id
            headerImage.setUid(uploadFileDTO.getUid());
            //创建时间
            headerImage.setCreateTime(new Date());
            headerImageMapper.insert(headerImage);
            saveHeaderImageDTO.setResult(RespCode.OK.name());
        } else {
            //头像存在
            //旧的组
            String oldGroup = headerImage.getGroupName();
            //旧的文件ID
            String oldFileId = headerImage.getFileId();
            //文件路径名称
            headerImage.setFileId(uploadFileDTO.getFieldId());
            //fdfs组名称
            headerImage.setGroupName(uploadFileDTO.getGroup());
            //扩展名
            headerImage.setExtName(uploadFileDTO.getExt());
            //头像高度
            headerImage.setImgHeight(uploadFileDTO.getHeight());
            //头像宽度
            headerImage.setImgWidth(uploadFileDTO.getWidth());
            //创建时间
            headerImage.setCreateTime(new Date());
            int result = headerImageMapper.updateById(headerImage);
            if (result == 1) {
                //更新成功
                saveHeaderImageDTO.setResult(RespCode.OK.name());
                saveHeaderImageDTO.setGroup(oldGroup);
                saveHeaderImageDTO.setFieldId(oldFileId);
            }
        }
        return saveHeaderImageDTO;
    }

    /**
     * 对原有的FastDFS中的文件进行删除操作
     *
     * @param group
     * @param fileId
     * @return
     */
    @Override
    public int deleteFileDFS(String group, String fileId) {
        return dfsClient.deleteFileDFS(group, fileId);
    }

    /**
     * 根据用户ID查询用户头像
     *
     * @param uid
     * @return
     */
    @Override
    public HeaderImage queryByUid(Integer uid) {
        return headerImageMapper.selectOne(new QueryWrapper<HeaderImage>().eq("uid", uid));
    }
}
